package com.ruoyi.wzgl.wzfl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.wzgl.wzfl.domain.Wzfl;
import com.ruoyi.wzgl.wzfl.service.IWzflService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 物资分类Controller
 *
 * @author huacai
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/wzgl/wzfl")
public class WzflController extends BaseController
{
    @Autowired
    private IWzflService wzflService;

    /**
     * 查询物资分类列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:list')")
    @GetMapping("/list")
    public AjaxResult list(Wzfl wzfl)
    {
        List<Wzfl> list = wzflService.selectWzflList(wzfl);
        return success(list);
    }

    /**
     * 导出物资分类列表
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:export')")
    @Log(title = "物资分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wzfl wzfl)
    {
        List<Wzfl> list = wzflService.selectWzflList(wzfl);
        ExcelUtil<Wzfl> util = new ExcelUtil<Wzfl>(Wzfl.class);
        util.exportExcel(response, list, "物资分类数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Wzfl> util = new ExcelUtil<Wzfl>(Wzfl.class);
        util.importTemplateExcel(response, "物资分类数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "物资分类", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Wzfl> util = new ExcelUtil<Wzfl>(Wzfl.class);
        InputStream inputStream = file.getInputStream();
        List<Wzfl> list = util.importExcel(inputStream );
        inputStream.close();
        int count = wzflService.batchInsertWzfl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取物资分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:query')")
    @GetMapping(value = "/{wzflId}")
    public AjaxResult getInfo(@PathVariable("wzflId") Integer wzflId)
    {
        return success(wzflService.selectWzflByWzflId(wzflId));
    }

    /**
     * 新增物资分类
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:add')")
    @Log(title = "物资分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wzfl wzfl)
    {
        return toAjax(wzflService.insertWzfl(wzfl));
    }

    /**
     * 修改物资分类
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:edit')")
    @Log(title = "物资分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wzfl wzfl)
    {
        return toAjax(wzflService.updateWzfl(wzfl));
    }

    /**
     * 删除物资分类
     */
    @PreAuthorize("@ss.hasPermi('wzgl:wzfl:remove')")
    @Log(title = "物资分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wzflIds}")
    public AjaxResult remove(@PathVariable Integer[] wzflIds)
    {
        return toAjax(wzflService.deleteWzflByWzflIds(wzflIds));
    }

    @GetMapping("/getWzflTree")
    public AjaxResult getWzflTree()
    {
        return success(wzflService.getWzflTree());
    }
}
